﻿using QLCHBANGIAY.DATA;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLCHBANGIAY.BUSINESS;

namespace QuanLyBanGiay
{
    public partial class timkiemsanpham : System.Web.UI.Page
    {
        string currentpage = "1";
        string key = "";
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["key"] != null) { key = Session["key"].ToString(); Session["key"] = key.ToString(); }
            if (!IsPostBack)
            {
                if (RouteData.Values["currentpage"] != null)
                {
                    currentpage = RouteData.Values["currentpage"].ToString();
                    BindData(currentpage, "12");
                }
                BindData(currentpage, "12");
                //show();
              //  show1();

            }
        }
        protected string Format_Price(string Price)
        {
            Price = Price.Replace(".", "");
            Price = Price.Replace(",", "");
            string tmp = "";
            while (Price.Length > 3)
            {
                tmp = "." + Price.Substring(Price.Length - 3) + tmp;
                Price = Price.Substring(0, Price.Length - 3);
            }
            tmp = Price + tmp;
            return tmp;
        }
        void show1()
        {
            string chuoi = "";
            List<Product> listpage = new List<Product>();
            listpage = ProductServices.Product_GetByTop("", Session["key"].ToString() + " and Active=1", "");
            chuoi += "<ul>";
            if (listpage.Count > 12)
            {
                if ((listpage.Count) % 12 == 0)
                {
                    if (Int32.Parse(currentpage) == 1)
                    {
                        chuoi += "<li><a  class='li_first' href='/Search/ket-qua.htm/page=1'>" + "Prev" + "</a></li>";
                    }
                    else
                    {
                        chuoi += "<li><a class='li_first' href='/Search/ket-qua.htm/page=" + (Int32.Parse(currentpage) - 1).ToString() + "'>" + "Prev" + "</a></li>";
                    }
                    for (int i = 1; i <= (listpage.Count / 12); i++)
                    {
                        if (i.ToString().Equals(currentpage))
                        {
                            chuoi += "<li><a class='selected tab' href='/Search/ket-qua.htm/page=" + i + "'>" + i + "</a></li>";
                        }
                        else
                        {
                            chuoi += "<li><a class='tab' href='/Search/ket-qua.htm/page=" + i + "'>" + i + "</a></li>";
                        }
                    }
                    if (Int32.Parse(currentpage) == ((listpage.Count / 12) + 1))
                    {
                        chuoi += "<li><a class='li_last' href='/Search/ket-qua.htm/page=" + currentpage + "'>" + "Next" + "</a></li>";
                    }
                    else
                    {
                        chuoi += "<li><a class='li_last' href='/Search/ket-qua.htm/page=" + (Int32.Parse(currentpage) + 1).ToString() + "'>" + "Next" + "</a></li>";
                    }
                }
                else
                {
                    if (Int32.Parse(currentpage) == 1)
                    {
                        chuoi += "<li><a class='li_first' href='/Search/ket-qua.htm/page=1'>" + "Prev" + "</a></li>";
                    }
                    else
                    {
                        chuoi += "<li><a class='li_first' href='/Search/ket-qua.htm/page=" + (Int32.Parse(currentpage) - 1).ToString() + "'>" + "Prev" + "</a></li>";
                    }

                    for (int i = 1; i <= (listpage.Count / 12) + 1; i++)
                    {
                        if (i.ToString().Equals(currentpage))
                        {
                            chuoi += "<li><a class='selected tab' href='/Search/ket-qua.htm/page=" + i + "'>" + i + "</a></li>";
                        }
                        else
                        {
                            chuoi += "<li><a class='tab' href='/Search/ket-qua.htm/page=" + i + "'>" + i + "</a></li>";
                        }
                    }
                    if (Int32.Parse(currentpage) == ((listpage.Count / 12) + 1))
                    {
                        chuoi += "<li><a class='li_last' href='/Search/ket-qua.htm/page=" + currentpage + "'>" + "Next" + "</a></li>";
                    }
                    else
                    {
                        chuoi += "<li><a class='li_last' href='/Search/ket-qua.htm/page=" + (Int32.Parse(currentpage) + 1).ToString() + "'>" + "Next" + "</a></li>";
                    }
                }

                //BindData(currentpage, "3");
            }
            else
            {
                string Chuoipro = "";
                if (listpage.Count > 0)
                {
                    for (int i = 0; i < listpage.Count; i++)
                    {
                        Chuoipro += " <div class='product_box'>";
                        Chuoipro += "<a href='productdetail.html'><img src='" + listpage[i].Image + "' alt='Shoes 1'></a>";
                        Chuoipro += "<p>" + listpage[i].Content + "</p>";
                        Chuoipro += "<p class='product_price'>$ " + listpage[i].Price + "</p>";
                        Chuoipro += " <a href='/productid='" + listpage[i].Id + " class='addtocart'></a>";
                        // <a href="./index.jsp?shop=<%=product.getId()%>" 
                        Chuoipro += " <a href='ChitietSanpham.aspx?id=" + listpage[i].Id + "' class='detail'></a>";
                        Chuoipro += " </div>";
                    }//for
                }
                ltrProduc.Text = Chuoipro;
                listpage.Clear();
                listpage = null;
            }
            chuoi += "</ul>";
            ltrpaging.Text = chuoi;

        }
        void BindData(string currentpage, string pagesize)
        {
            if (Session["key"] != null)
            {
                List<Product> list = new List<Product>();
                list = ProductServices.Product_Paging(currentpage, pagesize, Session["key"].ToString(), "");
                string Chuoi = "";
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        Chuoi += " <div class='product_box'>";
                        Chuoi += "<a href='productdetail.html'><img hight='250' width='200' src='" + list[i].Image + "' alt='Shoes 1'></a>";
                        Chuoi += "<p>" + list[i].Content + "</p>";
                        Chuoi += "<p class='product_price'>$ " + list[i].Price + "</p>";
                        Chuoi += " <a href='/productid='" + list[i].Id + " class='addtocart'></a>";
                        // <a href="./index.jsp?shop=<%=product.getId()%>" 
                        Chuoi += " <a href='ChitietSanpham.aspx?id=" + list[i].Id + "' class='detail'></a>";
                        Chuoi += " </div>";
                    }
                }
                ltrProduc.Text = Chuoi;
                list.Clear();
                list = null;
            }
        }
      
    
    }
}